package org.shoukaiseki.common.logger


import org.shoukaiseki.common.logger.level.LogbackLevelSetting
import org.slf4j.LoggerFactory
import java.util.function.Supplier

class LoggerSLF4JImpl :Logger{
    override fun isErrorEnabled(): Boolean {
        return logger.isErrorEnabled
    }

    override fun isInfoEnabled(): Boolean {
        return logger.isInfoEnabled
    }

    override fun isWarnEnabled(): Boolean {
        return logger.isWarnEnabled
    }

    override fun isDebugEnabled(): Boolean {
        return logger.isDebugEnabled
    }

    override fun errorLambar(loggerLambda: LoggerLambda?) {
        if(logger.isErrorEnabled){
            try {
                loggerLambda!!.run(this)
            }catch (e:Throwable){

            }
        }
    }

    override fun errorLambar(messageSupplier: Supplier<String>?) {
        if(logger.isErrorEnabled){
            logger.error(nullSafeGet(messageSupplier))
        }
    }

    override fun errorLambar(runnable: Runnable?) {
        if(logger.isErrorEnabled){
            try {
                runnable!!.run()
            }catch (e:Throwable){

            }
        }
    }

    override fun infoLambar(loggerLambda: LoggerLambda?) {
        if(logger.isInfoEnabled){
            try {
                loggerLambda!!.run(this)
            }catch (e:Throwable){

            }
        }
    }

    override fun infoLambar(messageSupplier: Supplier<String>?) {
        if(logger.isInfoEnabled){
            logger.info(nullSafeGet(messageSupplier))
        }
    }

    override fun infoLambar(runnable: Runnable?) {
        if(logger.isInfoEnabled){
            try {
                runnable!!.run()
            }catch (e:Throwable){

            }
        }
    }


    override fun warnLambar(loggerLambda: LoggerLambda?) {
        if(logger.isWarnEnabled){
            try {
                loggerLambda!!.run(this)
            }catch (e:Throwable){

            }
        }
    }

    override fun warnLambar(messageSupplier: Supplier<String>?) {
        if(logger.isWarnEnabled){
            logger.warn(nullSafeGet(messageSupplier))
        }
    }

    override fun warnLambar(runnable: Runnable?) {
        if(logger.isWarnEnabled){
            try {
                runnable!!.run()
            }catch (e:Throwable){

            }
        }
    }

    override fun debugLambar(loggerLambda: LoggerLambda?) {
        if(logger.isDebugEnabled){
            try {
                loggerLambda!!.run(this)
            }catch (e:Throwable){

            }
        }
    }

    override fun debugLambar(messageSupplier: Supplier<String>) {
        if(logger.isDebugEnabled){
            logger.debug(nullSafeGet(messageSupplier))
        }
    }

    override fun debugLambar(runnable: Runnable?) {
        if(logger.isDebugEnabled){
            try {
                runnable!!.run()
            }catch (e:Throwable){

            }
        }
    }

    private fun nullSafeGet(messageSupplier: Supplier<String>?): String{
        if (messageSupplier==null) {
            return ""
        }
        if (messageSupplier.get()!=null) {
            return messageSupplier.get()
        }
        return ""
    }


    private var logger: org.slf4j.Logger


    constructor(clazz: Any) {
        if(clazz is Class<*>){

//            println("logging.level.${clazz.name}")

            //默认开启debug
//            System.setProperty("org.slf4j.simpleLogger.defaultLogLevel","debug")
            logger = LoggerFactory.getLogger(clazz)
//            println("logger.javaClass.packageName=${logger.javaClass.packageName}")

            //默认开启debug
//            if(logger.javaClass.`package`.name=="ch.qos.logback.classic"){
//                LogbackLevelSetting.debugLevel(clazz.name)
//            }
        }else if(clazz is String){
            logger = LoggerFactory.getLogger(clazz)
        }else{
            logger = LoggerFactory.getLogger(clazz.toString())
        }


    }


    fun isTraceEnabled(): Boolean {
        return logger.isTraceEnabled
    }

    override fun getName(): String {
        return logger.name
    }

    override fun trace(var1: String?) {
        if(logger.isTraceEnabled)
            logger.trace(var1)
    }

    override fun trace(var1: String?, vararg var2: Any?) {
        if(logger.isTraceEnabled)
            logger.trace(var1,*var2)
    }

    override fun trace(var1: String?, var2: Throwable?) {
        if(logger.isTraceEnabled)
            logger.trace(var1,var2)
    }

    override fun debug(var1: String?) {
        if(logger.isDebugEnabled)
            logger.debug(var1)
    }

    override fun debug(var1: String?, vararg var2: Any?) {
        if(logger.isDebugEnabled)
            logger.debug(var1,*var2)
    }

    override fun debug(var1: String?, var2: Throwable?) {
        if(logger.isDebugEnabled)
            logger.debug(var1,var2)
    }

    override fun info(var1: String?) {
        if(logger.isInfoEnabled)
            logger.info(var1)
    }

    override fun info(var1: String?, vararg var2: Any?) {
        if(logger.isInfoEnabled)
            logger.info(var1,*var2)
    }

    override fun info(var1: String?, var2: Throwable?) {
        if(logger.isInfoEnabled)
            logger.info(var1,var2)
    }

    override fun warn(var1: String?) {
        if(logger.isWarnEnabled)
            logger.warn(var1)
    }

    override fun warn(var1: String?, var2: Any?) {
        if(logger.isWarnEnabled)
            logger.warn(var1,var2)
    }

    override fun warn(var1: String?, vararg var2: Any?) {
        if(logger.isWarnEnabled)
            logger.warn(var1,*var2)
    }

    override fun warn(var1: String?, var2: Throwable?) {
        if(logger.isWarnEnabled)
            logger.warn(var1,var2)
    }

    override fun error(var1: String?) {
        logger.error(var1)
    }

    override fun error(var1: String?, var2: Any?) {
        logger.error(var1,var2)
    }


    override fun error(var1: String?, vararg var2: Any?) {
        logger.error(var1,*var2)
    }

    override fun error(var1: String?, var2: Throwable?) {
        logger.error(var1,var2)
    }



}
